namespace 第四次作业
{
    public partial class Form1 : Form
    {

        private string currentDirectory;

        public Form1()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadDrives();
        }

        private void LoadDrives()
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                TreeNode driveNode = new TreeNode(drive.Name);
                driveNode.Tag = drive.RootDirectory.FullName;
                folderTreeView.Nodes.Add(driveNode);
            }
        }

        private void folderTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string? selectedPath = e.Node.Tag as string;
            if (!string.IsNullOrEmpty(selectedPath))
            {
                currentDirectory = selectedPath;
                LoadFilesAndFolders(selectedPath);
            }
        }

        private void LoadFilesAndFolders(string path)
        {
            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);

                fileListView.Items.Clear();

                foreach (DirectoryInfo directory in directoryInfo.GetDirectories())
                {
                    ListViewItem item = new ListViewItem(directory.Name, 0);
                    item.SubItems.Add("文件夹");
                    item.Tag = directory.FullName;
                    fileListView.Items.Add(item);
                }

                foreach (FileInfo file in directoryInfo.GetFiles())
                {
                    ListViewItem item = new ListViewItem(file.Name, 1);
                    item.SubItems.Add(file.Extension.ToUpper() + " 文件");
                    item.Tag = file.FullName;
                    fileListView.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void fileListView_DoubleClick(object sender, EventArgs e)
        {
            if (fileListView.SelectedItems.Count > 0)
            {
                string selectedPath = fileListView.SelectedItems[0].Tag as string;
                if (!string.IsNullOrEmpty(selectedPath))
                {
                    if (File.Exists(selectedPath))
                    {
                        string extension = Path.GetExtension(selectedPath).ToLower();
                        if (extension == ".exe")
                        {
                            RunExecutable(selectedPath);
                        }
                        else if (extension == ".txt")
                        {
                            OpenWithNotepad(selectedPath);
                        }
                        else
                        {
                            MessageBox.Show("不支持该文件类型的打开！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else if (Directory.Exists(selectedPath))
                    {
                        currentDirectory = selectedPath;
                        LoadFilesAndFolders(selectedPath);
                    }
                }
            }
        }

        private void RunExecutable(string path)
        {
            try
            {
                System.Diagnostics.Process.Start(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OpenWithNotepad(string path)
        {
            try
            {
                System.Diagnostics.Process.Start("notepad.exe", path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}